package com.data.codegenerate.generate.impl;

import com.data.codegenerate.config.CodeConfigProperties;
import com.data.codegenerate.database.DbReadTableUtil;
import com.data.codegenerate.generate.IGenerate;
import com.data.codegenerate.generate.config.CreateFileConfig;
import com.data.codegenerate.generate.impl.base.BaseCodeGenerate;
import com.data.codegenerate.generate.pojo.ColumnVo;
import com.data.codegenerate.generate.pojo.TableVo;
import com.data.codegenerate.generate.util.NonceUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeGenerateOne extends BaseCodeGenerate implements IGenerate {
    private static final Logger a = LoggerFactory.getLogger(CodeGenerateOne.class);
    private TableVo b;
    private List<ColumnVo> e;
    private List<ColumnVo> f;

    public CodeGenerateOne(TableVo tableVo) {
        this.b = tableVo;
    }

    public CodeGenerateOne(TableVo tableVo, List<ColumnVo> columns, List<ColumnVo> originalColumns) {
        this.b = tableVo;
        this.e = columns;
        this.f = originalColumns;
    }

    @Override // org.jeecgframework.codegenerate.generate.IGenerate
    public Map<String, Object> a() throws Exception {
        int i = -1;
        HashMap hashMap = new HashMap();
        hashMap.put("bussiPackage", CodeConfigProperties.bussiPackage);
        hashMap.put("entityPackage", this.b.getEntityPackage());
        hashMap.put("entityName", this.b.getEntityName());
        hashMap.put("tableName", this.b.getTableName());
        hashMap.put("primaryKeyField", CodeConfigProperties.dbTableId);
        if (this.b.getFieldRequiredNum() == null) {
            this.b.setFieldRequiredNum(Integer.valueOf(StringUtils.isNotEmpty(CodeConfigProperties.fieldRequiredNum) ? Integer.parseInt(CodeConfigProperties.fieldRequiredNum) : -1));
        }
        if (this.b.getSearchFieldNum() == null) {
            TableVo tableVo = this.b;
            if (StringUtils.isNotEmpty(CodeConfigProperties.pageSearchFiledNum)) {
                i = Integer.parseInt(CodeConfigProperties.pageSearchFiledNum);
            }
            tableVo.setSearchFieldNum(Integer.valueOf(i));
        }
        if (this.b.getFieldRowNum() == null) {
            this.b.setFieldRowNum(Integer.valueOf(Integer.parseInt(CodeConfigProperties.fieldRowNum)));
        }
        hashMap.put("tableVo", this.b);
        try {
            if (this.e == null || this.e.size() == 0) {
                this.e = DbReadTableUtil.a(this.b.getTableName());
            }
            hashMap.put("columns", this.e);
            if (this.f == null || this.f.size() == 0) {
                this.f = DbReadTableUtil.readOriginalTableColumn(this.b.getTableName());
            }
            hashMap.put("originalColumns", this.f);
            for (ColumnVo columnVo : this.f) {
                if (columnVo.getFieldName().toLowerCase().equals(CodeConfigProperties.dbTableId.toLowerCase())) {
                    hashMap.put("primaryKeyPolicy", columnVo.getFieldType());
                }
            }
            hashMap.put("serialVersionUID", String.valueOf(NonceUtils.c() + NonceUtils.g()));
            a.info("load template data: " + hashMap.toString());
            return hashMap;
        } catch (Exception e2) {
            throw e2;
        }
    }

    @Override // org.jeecgframework.codegenerate.generate.IGenerate
    public List<String> generateCodeFile(String stylePath) throws Exception {
        a.debug("----jeecg---Code----Generation----[单表模型:" + this.b.getTableName() + "]------- 生成中。。。");
        String str = CodeConfigProperties.projectPath;
        Map<String, Object> a2 = a();
        String str2 = CodeConfigProperties.templatepath;
        if (a(str2, "/").equals("conf/code-template")) {
            str2 = "/" + a(str2, "/") + "/one";
            CodeConfigProperties.getUrl(str2);
        }
        CreateFileConfig aVar = new CreateFileConfig(str2);
        aVar.a(stylePath);
        a(aVar, str, a2);
        a.info(" ----- jeecg-boot ---- generate  code  success =======> 表名：" + this.b.getTableName() + " ");
        return this.d;
    }

    @Override // org.jeecgframework.codegenerate.generate.IGenerate
    public List<String> generateCodeFile(String projectPath, String templatePath, String stylePath) throws Exception {
        if (projectPath != null && !"".equals(projectPath)) {
            CodeConfigProperties.getDriverName(projectPath);
        }
        if (templatePath != null && !"".equals(templatePath)) {
            CodeConfigProperties.getUrl(templatePath);
        }
        generateCodeFile(stylePath);
        return this.d;
    }

    public static void main(String[] args) {
        TableVo tableVo = new TableVo();
        tableVo.setTableName("demo");
        tableVo.setPrimaryKeyPolicy("uuid");
        tableVo.setEntityPackage("test");
        tableVo.setEntityName("JeecgDemo");
        tableVo.setFtlDescription("jeecg 测试demo");
        try {
            new CodeGenerateOne(tableVo).generateCodeFile(null);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
}
